package com.alex.megacj.checker;

import javax.servlet.http.HttpServletRequest;

import net.rubyeye.xmemcached.MemcachedClient;

import org.springframework.beans.factory.annotation.Required;

public class UserChecker {

	private MemcachedClient memcachedClient;
	
	@Required
	public void setMemcachedClient(MemcachedClient memcachedClient) {
		this.memcachedClient = memcachedClient;
	}
	
	public boolean isUserAllowed(HttpServletRequest request) {
		try {
			String ip = request.getHeader("x-forwarded-for");
			if (ip == null) {
				ip = request.getRemoteAddr();
			}
			String key = SecurityUtils.encodeMD5(ip + " " + request.getHeader("User-Agent"));
			return memcachedClient.get(key) != null;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
